package com.jiefutong.zyt.http.view;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by admin on 2017/2/11.
 */

public class RequestPageApiCallBack <T> implements Callback<T> {
    private IRequestPageView view;
    private PageVOConverter converter;
    private boolean isFirstPage;

    public RequestPageApiCallBack(boolean isFirstPage, IRequestPageView view, PageVOConverter pageVOConverter) {
        this.view = view;
        this.converter = pageVOConverter;
        this.isFirstPage = isFirstPage;
    }

    @Override
    public void onResponse(Call<T> call, Response<T> response) {
        if (response.isSuccessful()) {
            T v = (T) response.body();
            List list = converter.converter(v);
            String msg=converter.getMsg(v);
            boolean isHasData = list != null && list.size() > 0;
            boolean isHasNextPage = list != null && list.size() == CommonConstants.RECORD_COUNT;
            if (isHasData) {
                view.onSuccess(v, isHasNextPage, isFirstPage);
            } else {
                if(msg!=null&&msg.equals("未授权")){
                    view.onFailure("未授权", isFirstPage);
                }else{
                    view.onFailure("暂无数据", isFirstPage);
                }

            }
        } else {
            view.onFailure("加载失败：" + response.code(), isFirstPage);
        }
        view.afterRequest();
    }

    @Override
    public void onFailure(Call<T> call,Throwable t) {
        view.onFailure("加载失败：" + t.getMessage(), isFirstPage);
        //view.onFailure("获取失败，", isFirstPage );
        view.afterRequest();
    }
}
